package com.neuedu.security;


import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;

import javax.annotation.Resource;

@Configuration
public class  SecurityConfig extends WebSecurityConfigurerAdapter {
    //必须重写该方法
    @Resource
    RestNoToken restNoToken;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();
        registry.antMatchers("/user/index","/user/login")
                .permitAll()
                .anyRequest()
                .authenticated();
        //前后分离配置关闭session
        registry.and()
                .csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //自定义认证没有返回东西
        registry.and()
                .exceptionHandling()
                .authenticationEntryPoint(restNoToken);
    }
}
